package com.mes.eventhandler;

import java.util.List;

import javax.enterprise.event.Observes;

import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.dal.service.OBQuery;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;

import com.mes.manufacture.data.EmesoProcessMaterial;

public class EmesoProcessMaterialEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EmesoProcessMaterial.ENTITY_NAME) };
	
	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}
	
	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
	      return;
	    }
		final EmesoProcessMaterial material = (EmesoProcessMaterial)event.getTargetInstance();
		String productId = material.getProduct().getId();
		String processId = material.getEmesProcess().getId();
		OBDal obdal = OBDal.getInstance();
		OBQuery<EmesoProcessMaterial> obQuery = obdal.createQuery(EmesoProcessMaterial.class, "product.id = :productId and emesProcess.id=:processId ");
		obQuery.setNamedParameter("productId", productId);
		obQuery.setNamedParameter("processId", processId);
		List<EmesoProcessMaterial> list = obQuery.list();
		
		String language = OBContext.getOBContext().getLanguage().getLanguage();
		ConnectionProvider conn = new DalConnectionProvider(false);
		if(list.size()>1){
			obdal.rollbackAndClose();
			throw new OBException(Utility.messageBD(conn, "物料已存在！", language));
		}
		if(list.size()==1){
			if(!list.get(0).getId().equals(material.getId())){
				obdal.rollbackAndClose();
				throw new OBException(Utility.messageBD(conn, "物料已存在！", language));
			}
		}
	}
	
	public void onSave(@Observes EntityNewEvent event) throws Throwable {
		if (!isValidEvent(event)) {
	      return;
	    }
		final EmesoProcessMaterial material = (EmesoProcessMaterial)event.getTargetInstance();
		String productId = material.getProduct().getId();
		String processId = material.getEmesProcess().getId();
		OBDal obdal = OBDal.getInstance();
		OBQuery<EmesoProcessMaterial> obQuery = obdal.createQuery(EmesoProcessMaterial.class, "product.id = :productId and emesProcess.id=:processId ");
		obQuery.setNamedParameter("productId", productId);
		obQuery.setNamedParameter("processId", processId);
		List<EmesoProcessMaterial> list = obQuery.list();
		
		String language = OBContext.getOBContext().getLanguage().getLanguage();
		ConnectionProvider conn = new DalConnectionProvider(false);
		if(list.size()>0){
			obdal.rollbackAndClose();
			throw new OBException(Utility.messageBD(conn, "物料已存在！", language));
		}
	}
}
